package com.tsk.user.api.controller;

import com.tsk.base.exception.BusinessException;
import com.tsk.user.UserApplication;
import com.tsk.user.application.model.User;
import com.tsk.user.feign.ExampleFeignService;
import com.tsk.user.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/** 用户控制器UserController
 * @author: 黄嘉振
 * @create: 2023/1/20 9:44
 * @description:
 **/
@RestController
@RequestMapping("/user")
@Validated
@Slf4j
public class UserController {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ExampleFeignService exampleFeignService;
    @GetMapping(value = "/test/{id}", produces = "application/json;charset=UTF-8")
   // @SentinelResource("/test/{id}")
    public Object getUser(@PathVariable("id")  Integer id) {
        User  user = new User();
        user.setId(id.toString());
        if ((id & 1) == 0) {
            throw new BusinessException("mock exception!");
        }
        String s = userMapper.selectById(new BigDecimal(id)) + ": " + UserApplication.prot;
        log.info("s: " + s);
        log.debug("========debug: " + s);
        return s;
    }

    @GetMapping("/test1/{id}")
    // @SentinelResource("/test/{id}")
    public Object getBaseUser(@PathVariable("id")  Integer id) {
        return exampleFeignService.testFeign();
    }

}
